package com.yunc.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.controller.ShopBaseController;
import com.yunc.base.utils.R;
import com.yunc.base.utils.SecurityUtils;
import com.yunc.modules.shop.entity.ShopItem;
import com.yunc.modules.shop.entity.ShopItemSpecs;
import com.yunc.modules.shop.service.IShopItemService;
import com.yunc.modules.shop.service.IShopItemSpecsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 商品管理
 *
 * @author congcong tao
 * @date 2018/12/24 9:29
 */
@Slf4j
@RestController
@RequestMapping("shopItem")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShopItemController extends ShopBaseController {

    private final IShopItemService shopItemService;
    private final IShopItemSpecsService shopItemSpecsService;
    
    @GetMapping("/page")
    public R page(Page<ShopItem> page, ShopItem item){
        QueryWrapper<ShopItem> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(item.getTitle())) {
            qw.eq("title", item.getTitle());
        }
        setShop(qw);
        // 不查询content语句
        qw.select(ShopItem.class, i -> !i.getProperty().equals("content"));
        shopItemService.page(page, qw);
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/list")
    public R list(ShopItem item){
        QueryWrapper<ShopItem> qw = new QueryWrapper<>();
        setShop(qw);
        qw.select(ShopItem.class, i -> !i.getProperty().equals("content"));
        List<ShopItem> list = shopItemService.list(qw);
        return R.ok().put("list", list).put("item", item);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable String id){
        ShopItem item = shopItemService.getById(id);
        // 查询itemSpecs
        QueryWrapper<ShopItemSpecs> ew = new QueryWrapper<>();
        ew.eq("item_id", id);
        List<ShopItemSpecs> itemSpecsList = shopItemSpecsService.list(ew);
        return R.ok().put("item", item).put("itemSpecsList", itemSpecsList);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(ShopItem item){
        setShopId(item);
        item.setCreator(SecurityUtils.getUserId());
        item.setCreateTime(new Date());
        boolean flag = shopItemService.save(item);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @PutMapping(value = "/update")
    public R update(ShopItem item){
        boolean flag = shopItemService.updateById(item);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @PutMapping(value = "/updateSpecs")
    public R updateSpecs(ShopItem item){
        String id = item.getId();
        if (StringUtils.isNotEmpty(item.getItemSpecsJson())) {
            shopItemSpecsService.saveSpecs(id, item.getItemSpecsJson());
        }
        return R.ok();
    }
}
